package br.com.sorteiotimes.action;

import java.sql.SQLException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;

import br.com.sorteiotimes.DAO.UsuarioDAO;
import br.com.sorteiotimes.entity.Usuario;



@ManagedBean(name="controller")
@SessionScoped
public class Controller {
	
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateJPAPU");
	protected EntityManager entityManager = emf.createEntityManager();
	
	private String menu="homepage";
	private String login = null;
	private String senha =  null;
	private String logado = "nao";
	private String deslogado = "sim";
	private Boolean palpitou = false;
	private Boolean visualizar = true;
	
	private UsuarioDAO usuarioDAO = new UsuarioDAO();
	
	public void Logar(){		
		
		Usuario user = new Usuario();
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();  
		request.getSession().setAttribute("usuario", null);
		try {
			user = usuarioDAO.login(login, senha);  // user recebe valores de login e senhas		
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(user != null){
			HttpServletRequest request1 = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();  
			request1.getSession().setAttribute("usuario", user);
			logado = "sim";
			deslogado = "nao";
			visualizar = false;
			}
		}
		
	public void Palpitar(){
		palpitou = true;
	}


	public String getMenu() {
		return menu;
	}

	public void setMenu(String menu) {
		this.menu = menu;
	}

	public String getLogin() {
		return login;
	}

	public String getSenha() {
		return senha;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public String getLogado() {
		return logado;
	}

	public String getDeslogado() {
		return deslogado;
	}

	public void setLogado(String logado) {
		this.logado = logado;
	}

	public void setDeslogado(String deslogado) {
		this.deslogado = deslogado;
	}

	public Boolean getPalpitou() {
		return palpitou;
	}

	public void setPalpitou(Boolean palpitou) {
		this.palpitou = palpitou;
	}

	public Boolean getVisualizar() {
		return visualizar;
	}

	public void setVisualizar(Boolean visualizar) {
		this.visualizar = visualizar;
	}
}

